package logic

import (
	"context"
	"house/models"

	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectListLogic {
	return &CollectListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectListLogic) CollectList(in *houserpc.CollectListRequest) (*houserpc.CollectListResponse, error) {
	// todo: add your logic here and delete this line
	var houses []models.HouseData
	models.DB.Table("houses").
		Select("houses.*,classes.name as class_name,users.nick_name as user_name").
		Joins("left join classes on classes.id=houses.class_id").
		Joins("left join users on users.id=houses.user_id").
		Where("user_id=?", in.UserId).
		Find(&houses)
	var collectList []*houserpc.CollectList
	for _, house := range houses {
		list := houserpc.CollectList{
			Title:        house.Title,
			Price:        float32(house.Price),
			Context:      house.Context,
			UserName:     house.UserName,
			ClassName:    house.ClassName,
			CollectCount: int64(house.CollectCount),
			HouseId:      int64(house.ID),
		}
		collectList = append(collectList, &list)
	}
	return &houserpc.CollectListResponse{
		List: collectList,
	}, nil
}
